package tool

import (
	"GameGo/po"
	"golang.org/x/image/math/f64"
	"math"
)

func GetNearestEnemy(e []po.Enemy, fromPos f64.Vec2) f64.Vec2 {
	if len(e) == 0 {
		return f64.Vec2{}
	}
	minDist := math.MaxFloat64
	for i := range e {
		switch enemy := e[i].(type) {
		case *po.OrdinaryEnemy:
			enemyPos := f64.Vec2{enemy.X, enemy.Y}
			dx := enemyPos[0] - fromPos[0]
			dy := enemyPos[1] - fromPos[1]
			dist := math.Hypot(dx, dy)

			if dist < minDist {
				minDist = dist
				return enemyPos
			}
		case *po.EliteEnemy:
			enemyPos := f64.Vec2{enemy.X, enemy.Y}
			dx := enemyPos[0] - fromPos[0]
			dy := enemyPos[1] - fromPos[1]
			dist := math.Hypot(dx, dy)

			if dist < minDist {
				minDist = dist
				return enemyPos
			}
		case *po.Boss:
			enemyPos := f64.Vec2{enemy.X, enemy.Y}
			dx := enemyPos[0] - fromPos[0]
			dy := enemyPos[1] - fromPos[1]
			dist := math.Hypot(dx, dy)
			if dist < minDist {
				minDist = dist
				return enemyPos
			}

		}

	}
	return f64.Vec2{}
}
